<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px gw-flex-between"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
    <div>
      <el-form-item label="镜像名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="版本号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
    </div>
      <el-form-item>
        <el-button @click="resetQuery" plain type="primary">重置</el-button>
        <el-button @click="handleQuery" type="primary">查询</el-button>
        <div class="btn-ml">
          <el-button
            type="primary"
            @click="openForm('create')"
            v-hasPermi="['multiModel:large-model:create']"
          >
            新增镜像
          </el-button>
        </div>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-show="showFlag"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      :header-cell-style="{ 'background': '#CCEBE7', 'text-align':'center' }"
      :row-class-name="rowClassName"
      lazy
      row-key="id"
      :expanded-row-keys="expandedRowKeys"
      @expand-change="handleExpandChange"
      >
      <el-table-column label="序号" type="index" width="60px" align="center" />
      <el-table-column label="镜像名称" prop="name" />
      <el-table-column label="版本号" align="center" prop="version" />
      <el-table-column label="厂商名称" align="center" prop="type" />
      <el-table-column label="描述" align="center" prop="type" />
      <el-table-column
        label="上传时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        sortable 
        width="180px"
      />
      <el-table-column
        label="镜像大小"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        sortable
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('view', scope.row.id)"
            v-hasPermi="['multiModel:large-model:query']"
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['multiModel:large-model:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['multiModel:large-model:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 弹窗: 创建任务 -->
  <CreateEvaluationTask ref="formRef" @success="getList" />
  <!-- 弹窗: 评分 -->
  <JudgingRules  ref="judgingRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE, getDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { EvaluationTaskApi, EvaluationTaskVO } from '@/api/multiModel/evaluationtask'
import CreateEvaluationTask from './createEvaluation.vue'
import JudgingRules from './judgingRules.vue'

/** 评测任务 列表 */
defineOptions({ name: 'ModelManage' })

const message = useMessage() // 消息弹窗

const list = ref<EvaluationTaskVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  status: undefined,
})
const queryFormRef = ref() // 搜索的表单
let expandedRowKeys = ref([])
const showFlag = ref(true)
/** 查询列表 */
const getList = async () => {
  try {
    const data = await EvaluationTaskApi.getEvaluationTaskPage(queryParams)
    list.value = data.list
    list.value.forEach(l => {
      l.hasChildren = l.type ? true : false
      l.children = []
    })
    showFlag.value = false
    expandedRowKeys.value = []
    total.value = data.total
  } finally {
    showFlag.value = true
  }
}

const rowClassName = ({ row }) => {
  // 根据条件决定是否显示展开按钮
  return row.hasChildren ? 'show-expand' : 'hide-expand'
}
const taskStatus:object = {
  0: '待推理',
  1: '推理中',
  2: '推理完成',
  3: '推理失败',
  4:  '评分中',
  5: '评分完成',
  6: '评分失败'
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
/** 评分规则弹窗 */
const judgingRef = ref()
const openJudging = (id: number) => {
  judgingRef.value.open(id)
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, props.modelType, props.modelName, id)
}

/** 删除按钮操作 */
const delConfirmRef = ref()
const handleDelete = async (id: number) => {
  delConfirmRef.value.open(id, ['是否确定删除选中镜像?'])
}
/** 删除按钮操作 */
const deleteConfirm = async (id: number) => {
  try {
    await LargeModelApi.deleteLargeModel(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss">
.el-table {
  :deep(.hide-expand .el-table__expand-column .el-table__expand-icon) {
    display: none;
  }
  :deep(.show-expand .el-table__expand-column .el-table__expand-icon) {
    display: block;
  }
  :deep(.el-table__expanded-cell) {
    padding: 0 !important;
  }
}

</style>